package com.gopay.bis.dao.bankorder.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.bis.dao.bankorder.BankOrderDAO;
import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.bank.BankOrderLock;
import com.gopay.common.constants.bank.BankOrderStatus;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BankRouteLog;
import com.gopay.common.util.DateUtils;

/**
 * 银行流水DAO实现
 * 
 * @BankOrderDAOImpl.java
 * @author yaobo
 * @2013-2-5 下午03:34:53 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository(value = "bankOrderDAO")
public class BankOrderDAOImpl extends BaseDAOImpl<BankOrder, String> implements BankOrderDAO {

    @Override
    public int updateBackSuccess(String orderId) {
        String curDate = DateUtils.getCurrDate();

        StringBuffer hql = new StringBuffer();
        hql.append("update BankOrder set orderStatus = '" + BankOrderStatus.BANK_CALL_SUCCESS + "',");
        hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') " + ",");
        hql.append("tranFinishTime = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        return this.bulkUpdate(hql.toString());
    }

	@Override
	public int updateBackSuccess(String orderId, String bankId) {
		String curDate = DateUtils.getCurrDate();

		StringBuffer hql = new StringBuffer();
		hql.append("update BankOrder set orderStatus = '" + BankOrderStatus.BANK_CALL_SUCCESS + "',");
		hql.append("bankOrderId = '"+bankId+"' " + ",");
		hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') " + ",");
		hql.append("tranFinishTime = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
		hql.append("where orderId = '" + orderId + "'");

		return this.bulkUpdate(hql.toString());
	}
    
    @Override
    public int updateBackFail(String orderId) {
        String curDate = DateUtils.getCurrDate();

        StringBuffer hql = new StringBuffer();
        hql.append("update BankOrder set orderStatus = '" + BankOrderStatus.ORDER_FAIL + "',");
        hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') " + ",");
        hql.append("tranFinishTime = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        return this.bulkUpdate(hql.toString());
    }

    @Override
    public int updateBackVerifyFail(String orderId) {
        String curDate = DateUtils.getCurrDate();

        StringBuffer hql = new StringBuffer();
        hql.append("update BankOrder set orderStatus = '" + BankOrderStatus.VERIFY_FAIL + "',");
        hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        return this.bulkUpdate(hql.toString());
    }
    
    
    @Override
    public int updateQueryBackSuccess(String orderId) {
        String curDate = DateUtils.getCurrDate();

        StringBuffer hql = new StringBuffer();
        hql.append("update BankOrder set orderStatus = '" + BankOrderStatus.AUTO_CHECK_SUCCESS + "',");
        hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') " + ",");
        hql.append("tranFinishTime = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        return this.bulkUpdate(hql.toString());
    }

    @Override
    public int updateQueryBackVerifyFail(String orderId) {
        String curDate = DateUtils.getCurrDate();

        StringBuffer hql = new StringBuffer();
        hql.append("update BankOrder set orderStatus = '" + BankOrderStatus.VERIFY_FAIL + "',");
        hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        return this.bulkUpdate(hql.toString());
    }
    
    
    public List<BankOrder> getBankOrdersByGopayOrderId(String orderId) {
    	String hql = "from BankOrder b where b.orderId=:orderId";
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("orderId", orderId);
    	List<BankOrder> bankOrders = findByHql(hql, map);
    	return bankOrders;
    }

    @Override
	public int updateBpsCheckSuccess(String orderId) {
		 String currDate = DateUtils.format(DateUtils.getSystemDate());

	        StringBuffer hql = new StringBuffer();
	        hql.append("update BankOrder set orderStatus = :orderStatus,");
	        hql.append("lastCheckBillTime = to_date( :lastCheckBillTime ,'yyyy-mm-dd hh24:mi:ss'),");
	        hql.append("lastUpdTs = to_date( :lastUpdTs ,'yyyy-mm-dd hh24:mi:ss') ");
	        hql.append("where orderId = '" + orderId + "'");

	        Map<String, Object> params = new HashMap<String, Object>();
	        params.put("orderStatus", BankOrderStatus.BPS_CHECK_SUCCESS);
	        params.put("lastCheckBillTime", currDate);
	        params.put("lastUpdTs", currDate);

	        return this.updateByHql(hql.toString(), params);
	}

	@Override
	public int updateBpsCheckFail(String orderId) {
		String currDate = DateUtils.format(DateUtils.getSystemDate());

        StringBuffer hql = new StringBuffer();
        hql.append("update BankOrder set orderStatus = :orderStatus,");
        hql.append("lastCheckBillTime = to_date( :lastCheckBillTime ,'yyyy-mm-dd hh24:mi:ss'),");
        hql.append("lastUpdTs = to_date( :lastUpdTs ,'yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderStatus", BankOrderStatus.BPS_CHECK_FAIL);
        params.put("lastCheckBillTime", currDate);
        params.put("lastUpdTs", currDate);

        return this.updateByHql(hql.toString(), params);
	}

	@Override
	public int updateBpsCheckOther(String orderId) {
		String currDate = DateUtils.format(DateUtils.getSystemDate());

        StringBuffer hql = new StringBuffer();
        hql.append("update BankOrder set lastCheckBillTime = to_date( :lastCheckBillTime ,'yyyy-mm-dd hh24:mi:ss'),");
        hql.append("lastUpdTs = to_date( :lastUpdTs ,'yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("lastCheckBillTime", currDate);
        params.put("lastUpdTs", currDate);

        return this.updateByHql(hql.toString(), params);
	}

    @Override
    public int updateInAcctFail(String orderId) {
        String curDate = DateUtils.getCurrDate();

        StringBuffer hql = new StringBuffer();
        hql.append("update BankOrder set orderStatus = '" + BankOrderStatus.ENTER_ACCOUNT_FIAL + "',");
        hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        return this.bulkUpdate(hql.toString());
    }
    
    @Override
	public int updateFrontBackTime(String orderId) {
		String currDate = DateUtils.format(DateUtils.getSystemDate());

        StringBuffer hql = new StringBuffer();
        hql.append("update BankOrder set ");
        hql.append("lastUpdTs = to_date( :lastUpdTs ,'yyyy-mm-dd hh24:mi:ss') ,");
        hql.append("frontBackTime = to_date( :frontBackTime ,'yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("lastUpdTs", currDate);
        params.put("frontBackTime", currDate);

        return this.updateByHql(hql.toString(), params);
	}

    @Override
    public List<BankOrder> getBankOrdersByOriBankOrderId(String bankOrderId) {
        String hql = "from BankOrder b where b.oriOutOrderId=:oriOutOrderId";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("oriOutOrderId", bankOrderId);
        List<BankOrder> bankOrders = findByHql(hql, map);
        return bankOrders;
    }

    @Override
    public int updateAmountError(String orderId) {
        String curDate = DateUtils.getCurrDate();

        StringBuffer hql = new StringBuffer();
        hql.append("update BankOrder set orderStatus = '" + BankOrderStatus.AMOUNT_ERROR + "',");
        hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        return this.bulkUpdate(hql.toString());
    }

    @Override
    public void addBankRouteLog(BankRouteLog bankRouteLog) {
        this.getHibernateTemplate().save(bankRouteLog);
    }

	@Override
	public int updateQueryBackSuccess(BankOrder bankOrder,String bankOrderStatus) {
		 StringBuffer hql = new StringBuffer();
	        hql.append("update BankOrder set orderStatus = :orderStatus , ");
	        hql.append("totalCheckTimes = :totalCheckTimes ,");
	        hql.append("lastCheckBillTime = :lastCheckBillTime ,");
	        hql.append("tranFinishTime = :tranFinishTime,");
	        hql.append("lastUpdTs = :lastUpdTs ");
	        hql.append("where outOrderId = :outOrderId");

	        Map<String, Object> params = new HashMap<String, Object>();
	        params.put("orderStatus", bankOrderStatus);
	        params.put("totalCheckTimes", bankOrder.getTotalCheckTimes());
	        params.put("lastCheckBillTime", DateUtils.getSystemDate());
	        params.put("tranFinishTime", DateUtils.getSystemDate());
	        params.put("lastUpdTs", DateUtils.getSystemDate());
	        params.put("outOrderId", bankOrder.getOutOrderId());

	        int result = this.updateByHql(hql.toString(),params);

	        return result;
	}

	@Override
	public int updateQueryBackVerifyFail(BankOrder bankOrder) {
		  String currDate = DateUtils.format(DateUtils.getSystemDate());

	        StringBuffer hql = new StringBuffer();
	        hql.append("update BankOrder set orderStatus = :orderStatus , ");
	        hql.append("totalCheckTimes = :totalCheckTimes ,");
	        if (BisBankBusinessProd._12.value.equals(bankOrder.getBankBizProdCode())) {
	            // 退款产品记录失败原因 add by zhengxue 20160909
	            hql.append("remark2 = :remark2 ,");
	        }
	        hql.append("lastCheckBillTime = to_date( :lastCheckBillTime ,'yyyy-mm-dd hh24:mi:ss'),");
	        hql.append("tranFinishTime = to_date( :tranFinishTime ,'yyyy-mm-dd hh24:mi:ss'),");
	        hql.append("lastUpdTs = to_date( :lastUpdTs ,'yyyy-mm-dd hh24:mi:ss') ");
	        hql.append("where outOrderId = :outOrderId");

	        Map<String, Object> params = new HashMap<String, Object>();
	        params.put("orderStatus", BankOrderStatus.VERIFY_FAIL);
	        params.put("totalCheckTimes", bankOrder.getTotalCheckTimes());
	        if (BisBankBusinessProd._12.value.equals(bankOrder.getBankBizProdCode())) {
	            // 退款产品记录失败原因 add by zhengxue 20160909
	            params.put("remark2", bankOrder.getRemark2());
	        }
	        params.put("lastCheckBillTime", currDate);
	        params.put("tranFinishTime", currDate);
	        params.put("lastUpdTs", currDate);
	        params.put("outOrderId", bankOrder.getOutOrderId());

	        return this.updateByHql(hql.toString(), params);
	}
	
	
	 @Override
	    public int lock(BankOrder bankOrder) {
	        String currDate = DateUtils.format(DateUtils.getSystemDate());

	        StringBuffer hql = new StringBuffer();
	        hql.append("update BankOrder set lock = :lock");
	        hql.append(",lastUpdTs = to_date( :lastUpdTs ,'yyyy-mm-dd hh24:mi:ss') ");
	        hql.append("where outOrderId = :outOrderId");
			 //add by zyt,2017-10-10,增加当前锁定条件的判断，
			 hql.append(" and lock = :curLock");

	        Map<String, Object> params = new HashMap<String, Object>();
	        params.put("lock", BankOrderLock.LOCK);
	        params.put("lastUpdTs", currDate);
	        params.put("outOrderId", bankOrder.getOutOrderId());
			 //add by zyt,2017-10-10,当前为未锁定的时候，才加锁
			 params.put("curLock", BankOrderLock.UN_LOCK);

	        return this.updateByHql(hql.toString(), params);
	    }

	    @Override
	    public int unlock(BankOrder bankOrder) {
	        String currDate = DateUtils.format(DateUtils.getSystemDate());

	        StringBuffer hql = new StringBuffer();
	        hql.append("update BankOrder set lock = :lock");
	        hql.append(",lastUpdTs = to_date( :lastUpdTs ,'yyyy-mm-dd hh24:mi:ss') ");
	        hql.append("where outOrderId = :outOrderId");
			//add by zyt,2017-10-10,增加当前锁定条件的判断，
			hql.append(" and lock = :curLock");

	        Map<String, Object> params = new HashMap<String, Object>();
	        params.put("lock", BankOrderLock.UN_LOCK);
	        params.put("lastUpdTs", currDate);
	        params.put("outOrderId", bankOrder.getOutOrderId());
			//add by zyt,2017-10-10,当前为锁定的时候，才解锁
			params.put("curLock", BankOrderLock.LOCK);

	        return this.updateByHql(hql.toString(), params);
	    }
	    
	    @Override
	    public int updateTotalCheckTimes(BankOrder bankOrder) {
	    	StringBuffer hql = new StringBuffer();
	    	hql.append("update BankOrder set ");
	    	hql.append("totalCheckTimes = :totalCheckTimes ,");
	    	hql.append("lastUpdTs = :lastUpdTs ");
	    	hql.append("where outOrderId = :outOrderId");
	    	
	    	Map<String, Object> params = new HashMap<String, Object>();
	    	params.put("totalCheckTimes", bankOrder.getTotalCheckTimes() + 1);
	    	params.put("lastUpdTs", DateUtils.getSystemDate());
	    	params.put("outOrderId", bankOrder.getOutOrderId());
	    	
	    	int result = this.updateByHql(hql.toString(),params);
	    	
	    	return result;
	    }
	    
	    @Override
	    public int updateCheckFail(BankOrder bankOrder) {
	        String currDate = DateUtils.format(DateUtils.getSystemDate());

	        StringBuffer hql = new StringBuffer();
	        hql.append("update BankOrder set orderStatus = :orderStatus , ");
	        hql.append("totalCheckTimes = :totalCheckTimes ,");
	        if (BisBankBusinessProd._12.value.equals(bankOrder.getBankBizProdCode())) {
	            // 退款产品记录失败原因 add by zhengxue 20160909
	            hql.append("remark2 = :remark2 ,");
	        }
	        hql.append("lastCheckBillTime = to_date( :lastCheckBillTime ,'yyyy-mm-dd hh24:mi:ss'),");
	        hql.append("tranFinishTime = to_date( :tranFinishTime ,'yyyy-mm-dd hh24:mi:ss'),");
	        hql.append("lastUpdTs = to_date( :lastUpdTs ,'yyyy-mm-dd hh24:mi:ss') ");
	        hql.append("where outOrderId = :outOrderId");

	        Map<String, Object> params = new HashMap<String, Object>();
	        params.put("orderStatus", BankOrderStatus.FAIL);
	        params.put("totalCheckTimes", bankOrder.getTotalCheckTimes());
	        if (BisBankBusinessProd._12.value.equals(bankOrder.getBankBizProdCode())) {
	            // 退款产品记录失败原因 add by zhengxue 20160909
	            params.put("remark2", bankOrder.getRemark2());
	        }
	        params.put("lastCheckBillTime", currDate);
	        params.put("tranFinishTime", currDate);
	        params.put("lastUpdTs", currDate);
	        params.put("outOrderId", bankOrder.getOutOrderId());

	        return this.updateByHql(hql.toString(), params);
	    }

		@Override
		public int updateBankOrderId(String outOrderId, String bankOrderId) {

		        StringBuffer hql = new StringBuffer();
		        hql.append("update BankOrder set bankOrderId = :bankOrderId ");
		        hql.append("where outOrderId = :outOrderId");

		        Map<String, Object> params = new HashMap<String, Object>();
		        params.put("bankOrderId", bankOrderId);
		        params.put("outOrderId", outOrderId);
		        return this.updateByHql(hql.toString(), params);
		}
}
